package com.example.easeimpro.controller.adapter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.InviteInfo;
import com.example.easeimpro.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class NewFriendListAdapter extends RecyclerView.Adapter<NewFriendListAdapter.Holder> {

    private final Context mContext;
    private OnInviteListener mOnInviteListener;
    private List<InviteInfo> mInviteData = new ArrayList<>();
    private InviteInfo info;
    private Handler handler = new Handler();

    public NewFriendListAdapter(Context context,OnInviteListener onInviteListener){
        mContext = context;
        mOnInviteListener = onInviteListener;
    }

    //刷新数据的方法
    public void refresh(List<InviteInfo> InviteDatas){

        if(InviteDatas != null && InviteDatas.size() >= 0){
            mInviteData.clear();//清空
            mInviteData.addAll(InviteDatas);
            notifyDataSetChanged();

        }
    }
    public void deleteRefresh(List<InviteInfo> InviteDatas,int pos){

        if(InviteDatas != null && InviteDatas.size() >= 0){
            mInviteData.clear();//清空
            mInviteData.addAll(InviteDatas);
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyItemRemoved(pos);
            }
        });


        }
    }
    private static final String TAG = "NewFriendListAdapter";
    @NonNull
    @Override
    public NewFriendListAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(mContext).inflate(R.layout.newfriend_list_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull NewFriendListAdapter.Holder holder, int position) {
        info = mInviteData.get(position);
        int mPos = position;
        UserInfo user = info.getUser();

        if(user != null){//联系人
            holder.mUser.setText(user.getName());
            holder.mBtnAccept.setVisibility(View.GONE);
            holder.mBtnReject.setVisibility(View.GONE);
            //原因
            Log.e(TAG, "onBindViewHolder: " +info.getReason());
            if(info.getStatus() == InviteInfo.InvationStatus.NEW_INVITE){//新的邀请

                if(info.getReason() == null){
                    holder.mReason.setText("添加好友");
                }else{
                    holder.mReason.setText(info.getReason());
                }
                holder.mBtnAccept.setVisibility(View.VISIBLE);
                holder.mBtnReject.setVisibility(View.VISIBLE);
            }else if(info.getStatus() == InviteInfo.InvationStatus.INVITE_ACCEPT){//接受邀请
                if(info.getReason() == null){
                    holder.mReason.setText("接受邀请");
                }else{
                    holder.mReason.setText(info.getReason());
                }
            }else if(info.getStatus() == InviteInfo.InvationStatus.INVITE_ACCEPT_BY_PEER){//邀请被接受
                if(info.getReason() == null){
                    holder.mReason.setText("邀请被接受");
                }else{
                    holder.mReason.setText(info.getReason());
                }
            }
            holder.mReason.setText(info.getReason());
        }else {//群组

        }

        //接受按钮的处理
        holder.mBtnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnInviteListener.onAccept(info,mPos);
            }
        });
        //拒绝按钮处理
        holder.mBtnReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnInviteListener.onReject(info,mPos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mInviteData == null? 0:mInviteData.size();//如果为空则返回0条数据
    }

    public class Holder extends RecyclerView.ViewHolder {
        private TextView mUser,mReason;
        private Button mBtnAccept;
        private Button mBtnReject;
        public Holder(@NonNull View itemView) {
            super(itemView);
            mUser = itemView.findViewById(R.id.new_friends_list_username);
            mReason = itemView.findViewById(R.id.new_friends_list_reason);
            mBtnAccept = itemView.findViewById(R.id.new_friends_list_accept);
            mBtnReject = itemView.findViewById(R.id.new_friends_list_reject);
        }
    }

    public interface OnInviteListener{
        //联系人接受按钮事件
        void onAccept(InviteInfo info,int position);
        //联系人拒绝按钮事件
        void onReject(InviteInfo info,int position);
    }
}
